package com.example.flowermall.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/user/**","/user","/user/{id}","/user/lists","/user/register").permitAll()
                .antMatchers("/user/login","/user/info","/vscode","/user/uploads/**","/menu","/role/all","/images/*").permitAll()
                .antMatchers("/role/list","/role","/role/{id}","/profile/**").permitAll()
                .antMatchers("/goods/**","/category/**","/category/all").permitAll()
                .antMatchers("/shoppingCart/**","/indexImg/**","/tag/**","/order/**","/userAddress/**","/productImg/**").permitAll()
                .antMatchers("/collection/**").permitAll()
                .antMatchers("/alipay/**","/dataTotal/**").permitAll()
                .antMatchers("/wx/**").permitAll()
                .anyRequest().authenticated();
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);


    }
}
